C++는 다음의 집합을 정의합니다: 기본 산술 타입 하드웨어 저장소에 직접 매핑됩니다. 메모리를 번호가 붙은 순서로 상상해 보세요. 주소. 각각의 바이트 (8비트)는 주소 지정 가능한 가장 작은 단위이며, 한 개의 워드 (일반적으로 32/64비트)는 자연스러운 처리 크기입니다.
1. 정수 타입
불린(예:bool)와 문자(유니코드 포함 char16_t, char32_t) 및 정수를 포함합니다. 정수는 부호 있는 (음수, 0, 양수를 나타냄) 또는 부호 없는 (값 ≥ 0)
2. 메모리 표현 방식
한 개의 int 워드를 차지하면 여러 바이트 주소에 걸쳐집니다. 예를 들어, 주소 736424에서 시작하는 객체는 연속된 4바이트의 메모리를 차지합니다.
3. 부동 소수점 및 빈 타입
부동 소수점 타입(float, double, long double)는 머신 비트 패턴을 통해 실수를 표현합니다. 그리고 void 타입은 '빈' 결과를 의미하며, 값이 없으며 일반 변수 선언에 사용될 수 없습니다.
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the primary difference between signed and unsigned integral types?
Signed types can represent negative values, while unsigned types are always ≥ 0.
Unsigned types use fewer bits to store numbers.
Signed types are only for floating-point numbers.
There is no difference in modern hardware.
✅ Correct!
Correct! Signed types allocate one bit to represent the sign (+/-), whereas unsigned types use all bits for the magnitude.❌ Incorrect
Signed types divide the bit pattern between negative and positive ranges; unsigned types represent only non-negative values.QUESTION 2
Which of these is considered a 'Unicode character type' in C++?
short
char32_t
long long
void
✅ Correct!
C++ provides char16_t and char32_t specifically to support international character sets like Unicode.❌ Incorrect
Types like wchar_t, char16_t, and char32_t are used for extended character sets.QUESTION 3
What happens if you assign -1 to an unsigned 8-bit char?
The program crashes.
The value becomes 255 due to modulo arithmetic.
The value is clamped to 0.
The value remains -1.
✅ Correct!
Unsigned types perform modulo arithmetic; -1 wraps to the maximum value of the type (2^8 - 1 = 255).❌ Incorrect
C++ unsigned arithmetic 'wraps around' using the remainder of the value modulo the number of values the type can hold.QUESTION 4
What is the role of the 'void' type?
To store small integers.
To represent the absence of a value or a generic pointer.
To define constants that cannot change.
To handle boolean logic.
✅ Correct!
Void is a special-purpose empty type used when no value is returned or when dealing with generic memory addresses.❌ Incorrect
Void cannot define variables; it indicates an absence of data.QUESTION 5
Which C++ standard guarantees apply to floating-point precision?
double must have at least as much precision as float.
float is always 32 bits and double is always 128 bits.
All floating-point types have exactly 10 significant digits.
Floating point types cannot represent zero.
✅ Correct!
Correct. C++ guarantees that float <= double <= long double in terms of precision.❌ Incorrect
C++ guarantees a hierarchy of precision (double ≥ float), not fixed bit sizes across all platforms.Exercise 2.5: Literal Classification & Data Modeling
Applying type logic to literals and real-world scenarios.
A financial application needs to calculate mortgage payments and handle international text identifiers. You must identify the correct C++ types to maintain precision and compatibility.
Q
1. To calculate a mortgage payment, what types would you use for the rate, principal, and payment? Why?
Solution:
You should use double or long double for all three. Mortgage calculations require high precision to minimize rounding errors over 15-30 year spans. While 'float' is smaller, 'double' typically offers 10+ significant digits, which is standard for financial math.
You should use double or long double for all three. Mortgage calculations require high precision to minimize rounding errors over 15-30 year spans. While 'float' is smaller, 'double' typically offers 10+ significant digits, which is standard for financial math.
Q
2. Determine the types of these literals: (a) 'a' (b) L'a' (c) 10u (d) 3.14L
Solution:
(a) char (character literal), (b) wchar_t (wide character literal), (c) unsigned int (unsigned decimal), (d) long double (extended-precision floating point).
(a) char (character literal), (b) wchar_t (wide character literal), (c) unsigned int (unsigned decimal), (d) long double (extended-precision floating point).
Q
3. Explain the difference between short, int, long, and long long.
Solution:
These are all integral types but differ in their minimum bit size. In C++: short (16 bits), int (16 bits, usually 32), long (32 bits), and long long (64 bits). This allows developers to choose a size based on memory constraints or the range of values needed.
These are all integral types but differ in their minimum bit size. In C++: short (16 bits), int (16 bits, usually 32), long (32 bits), and long long (64 bits). This allows developers to choose a size based on memory constraints or the range of values needed.